{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "DzOrCdr7dnvw"
      },
      "outputs": [],
      "source": [
        "!pip install gradio bypy huggingface_hub"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import os\n",
        "import shutil\n",
        "\n",
        "from huggingface_hub import snapshot_download\n",
        "\n",
        "from bypy import ByPy"
      ],
      "metadata": {
        "id": "rz1yHJ1ndqiI"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### 下载模型"
      ],
      "metadata": {
        "id": "ztUNZKSSmjw9"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def copy_data(source_directory,target_directory):\n",
        "  # 遍历源目录中的所有文件和目录\n",
        "  for entry in os.listdir(source_directory):\n",
        "      entry_path = os.path.join(source_directory, entry)\n",
        "      # 检查是否为符号链接\n",
        "      if os.path.islink(entry_path):\n",
        "          # 获取符号链接的目标路径\n",
        "          target_path = os.readlink(entry_path)\n",
        "          # 构建目标路径的绝对路径\n",
        "          target_absolute_path = os.path.join(source_directory, target_path)\n",
        "          # 获取目标路径的文件名或目录名\n",
        "          target_basename = os.path.basename(target_absolute_path)\n",
        "          # 构建目标文件或目录的路径\n",
        "          target_entry_path = os.path.join(target_directory, entry)\n",
        "\n",
        "          # 复制目标文件或目录到目标目录\n",
        "          if os.path.isdir(target_absolute_path):\n",
        "              shutil.copytree(target_absolute_path, target_entry_path)\n",
        "          else:\n",
        "              shutil.copy2(target_absolute_path, target_entry_path)\n",
        "      else:\n",
        "          # 如果不是符号链接，直接复制到目标目录\n",
        "          shutil.copy2(entry_path, os.path.join(target_directory, entry))"
      ],
      "metadata": {
        "id": "MtWdFSBkhNjg"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def download(repo_id):\n",
        "    source_directory = snapshot_download(repo_id=repo_id,cache_dir=\"/app/data/\",force_download =True)\n",
        "    print(\"文件路径:\",source_directory)\n",
        "    path_name = repo_id.replace(\"/\",\"---\")\n",
        "    target_directory = f\"{os.getcwd()}/download/{path_name}\"\n",
        "    # 移动文件\n",
        "    os.system(f\"mkdir -p {target_directory}\")\n",
        "    os.system(f\"mkdir -p {os.getcwd()}/tar/{path_name}/\")\n",
        "    copy_data(source_directory,target_directory)\n",
        "    print(\"复制路径：\",target_directory)\n",
        "\n",
        "    return target_directory"
      ],
      "metadata": {
        "id": "oQerA_qgd6jX"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "## 设置 repo_id\n",
        "repo_id = \"THUDM/chatglm2-6b-int4\""
      ],
      "metadata": {
        "id": "RLI1nAYDe4Qp"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "out_path = download(repo_id)"
      ],
      "metadata": {
        "id": "79yTz_HifKLX"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "out_path"
      ],
      "metadata": {
        "id": "RFPzBVf0f0HI"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### 上传百度网盘\n",
        "\n",
        "百度云开发者平台： https://pan.baidu.com/union\n",
        "\n",
        "大小限制\n",
        "\n",
        "所有开发者均可接入使用接口，但可上传单个文件大小根据授权用户的身份有不同的限制:\n",
        "\n",
        "- 普通用户单个上传文件大小上限为4GB\n",
        "- 会员用户单个上传文件大小上限为10GB\n",
        "- 超级会员用户单个上传文件大小上限为20GB"
      ],
      "metadata": {
        "id": "6E8bLX2hlpeZ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "def upload_by_file(source_path):\n",
        "    bp = ByPy()\n",
        "    dest_path = source_path.split(\"/\")[-1]  # 文件名\n",
        "    out = bp.upload(\n",
        "      source_path,\n",
        "      dest_path\n",
        "    )\n",
        "    return out\n",
        "def upload_by_path(source_path,compress=True):\n",
        "    bp = ByPy()\n",
        "    # 创建目录\n",
        "    dest_path = source_path.split(\"/\")[-1]\n",
        "    # 压缩\n",
        "    if compress:\n",
        "      print(\"分卷压缩....\")\n",
        "      tar_path =f\"{os.getcwd()}/tar/{dest_path}\"\n",
        "      os.system(f\"zip -s 4000M {tar_path}/{dest_path}.zip {source_path}/*\")\n",
        "      source_path = tar_path\n",
        "    print(\"上传百度云....\")\n",
        "    bp.mkdir(dest_path)\n",
        "    # 同步目录\n",
        "    out = bp.syncup(\n",
        "      source_path,\n",
        "      dest_path\n",
        "    )\n",
        "    return out"
      ],
      "metadata": {
        "id": "2OSQjojXmYQQ"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "bp = ByPy()\n",
        "print(bp.list())"
      ],
      "metadata": {
        "id": "7Ujr2x48enZ4"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# 上传文件夹\n",
        "# 如果单个文件大小小于 4G 则无需压缩\n",
        "upload_by_path(out_path,compress=False)"
      ],
      "metadata": {
        "id": "t4n7pypIif9D"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "### 删除模型"
      ],
      "metadata": {
        "id": "cesLXhVim8E4"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!rm -rf /content/download/*\n",
        "!rm -rf /content/tar/*\n"
      ],
      "metadata": {
        "id": "23mk0w5Uk17I"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "v16kEKUtsu-K"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}